from django.db import models
from core.mixins import TimeStampedMixin
from django.conf import settings

# Create your models here.

class Category(TimeStampedMixin):
    name = models.CharField(max_length=100, verbose_name='分类名称')
    description = models.TextField(blank=True, verbose_name='分类描述')
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, related_name='children', verbose_name='父分类')
    order = models.IntegerField(default=0, verbose_name='排序')

    class Meta:
        verbose_name = '分类'
        verbose_name_plural = '分类'
        ordering = ['order', 'id']

    def __str__(self):
        return self.name

class Tag(TimeStampedMixin):
    name = models.CharField(max_length=50, unique=True, verbose_name='标签名称')
    description = models.TextField(blank=True, verbose_name='标签描述')

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = '标签'

    def __str__(self):
        return self.name

class Link(TimeStampedMixin):
    title = models.CharField(max_length=200, verbose_name='链接标题')
    url = models.URLField(verbose_name='URL地址')
    description = models.TextField(blank=True, verbose_name='链接描述')
    icon = models.URLField(blank=True, verbose_name='图标URL')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='links', verbose_name='所属分类')
    tags = models.ManyToManyField(Tag, blank=True, related_name='links', verbose_name='标签')
    visit_count = models.IntegerField(default=0, verbose_name='访问次数')
    is_active = models.BooleanField(default=True, verbose_name='是否激活')

    class Meta:
        verbose_name = '链接'
        verbose_name_plural = '链接'
        ordering = ['-visit_count', '-created_at']

    def __str__(self):
        return self.title

class UserVisit(TimeStampedMixin):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='visits', verbose_name='用户')
    link = models.ForeignKey(Link, on_delete=models.CASCADE, related_name='user_visits', verbose_name='访问链接')
    
    class Meta:
        verbose_name = '访问记录'
        verbose_name_plural = '访问记录'
        ordering = ['-created_at']

class UserPreference(TimeStampedMixin):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='preference', verbose_name='用户')
    favorite_categories = models.ManyToManyField(Category, blank=True, related_name='preferred_by', verbose_name='喜好分类')
    favorite_tags = models.ManyToManyField(Tag, blank=True, related_name='preferred_by', verbose_name='喜好标签')
    theme = models.CharField(max_length=50, default='default', verbose_name='主题')
    layout = models.CharField(max_length=50, default='grid', verbose_name='布局')
    items_per_page = models.IntegerField(default=20, verbose_name='每页显示数量')

    class Meta:
        verbose_name = '用户偏好'
        verbose_name_plural = '用户偏好'

    def __str__(self):
        return f"{self.user.username}的偏好设置"

class Favorite(TimeStampedMixin):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='favorites', verbose_name='用户')
    link = models.ForeignKey(Link, on_delete=models.CASCADE, related_name='favorited_by', verbose_name='收藏链接')

    class Meta:
        verbose_name = '收藏'
        verbose_name_plural = '收藏'
        ordering = ['-created_at']
        unique_together = ['user', 'link']  # Prevent duplicate favorites

    def __str__(self):
        return f"{self.user.username} - {self.link.title}"
